package 程序员面试金典;

public class 反转单词前缀 {
    public String reversePrefix(String word, char ch) {
        if (word.charAt(0)==ch)
            return word;
        int idx=0;
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i)==ch){
                idx=i;
                break;
            }
        }
        //ch不在word中
        if (idx==0)
            return word;
        String s = word.substring(0, idx + 1);
        StringBuilder builder = new StringBuilder();
        for (int i = s.length()-1; i >=0 ; i--) {
            builder.append(s.charAt(i));
        }
        for (int i = idx+1; i < word.length(); i++) {
            builder.append(word.charAt(i));
        }
        return builder.toString();
    }
}
